package com.example.yls.demo5;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.ArrayList;

/**
 * Created by yls on 2018/10/22.
 */

public class StuAdapter extends RecyclerView.Adapter<StuViewHolder> {
    private Context context;
    private ArrayList<Student> studentArrayList;
/*    private ArrayList<Student> searchArrayList;*/
    private OnItemDelClickListener listener1;
    private OnItemEditClickListener listener2;
    private boolean resultList;

    public StuAdapter(Context context, ArrayList<Student> studentArrayList,/*ArrayList<Student> searchArrayList,*/OnItemDelClickListener listener1,OnItemEditClickListener listener2,boolean resultList) {
        this.context = context;
        this.studentArrayList = studentArrayList;
/*        this.searchArrayList = searchArrayList;*/
        this.listener1=listener1;
        this.listener2=listener2;
        this.resultList=resultList;
    }

    @Override
    public int getItemCount() {
        return studentArrayList.size();
    }

    @Override
    public StuViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.list_item,parent,false);

        StuViewHolder viewHolder = new StuViewHolder(view,null);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(StuViewHolder holder, final int position) {
        final Student s = studentArrayList.get(position);

        holder.headView.setImageResource(s.getImgId());
        holder.nameView.setText(s.getName());
        holder.ageView.setText(String.valueOf(s.getAge()));


        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context, s.getName()+" "+ s.getAge(),Toast.LENGTH_SHORT).show();

                listener2.Edit(position,view);

            }
        });

        holder.delView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener1.del(position);
            }
        });


    }

    public void changeData(ArrayList<Student> studentArrayList){
        this.studentArrayList = studentArrayList;
        notifyDataSetChanged();
    }

}
